﻿using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System;

namespace Adams.Public.DataAccess
{
    public static class Events
    {
        public static Adams.Public.DataTransfer.Events.EventCollection GetEvents(Adams.Public.DataTransfer.Events.GetEventsInput input)
        {
            var data = new Adams.Public.DataTransfer.Events.EventCollection();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT O.* , C.Color
FROM Events.Occurances O
INNER JOIN Events.Events E ON O.EventID = E.EventID
INNER JOIN Events.Calendars C ON C.CalendarID = E.CalendarID
WHERE DATEDIFF(d, @StartDate, O.StartDate) >= 0 AND DATEDIFF(d, O.StartDate, @EndDate) >= 0
", CommandType.Text,
    DB.CreateParameter("@StartDate", input.Start),
    DB.CreateParameter("@EndDate", input.End)))
            {
                while (dr.Read())
                {
                    var e = new Adams.Public.DataTransfer.Events.Event();
                    e.id = dr["OccuranceID"].DbInt();
                    e.eventid = dr["EventID"].DbInt();
                    e.title = dr["Title"].DbString();
                    e.start = dr["StartDate"].DbDate().ToString();
                    e.end = dr["EndDate"].DbDate().ToString();
                    e.allDay = dr["IsAllDay"].DbBool();
                    e.color = dr["Color"].DbString();
                    data.Add(e);
                }
            }

            return data;
        }

        public static Adams.Shared.Entities.Events.Occurance GetEvent(Adams.Public.DataTransfer.Events.GetEventInput input)
        {
            var data = new Adams.Shared.Entities.Events.Occurance();

            using (SqlDataReader dr = DB.RunDataReader(@"
DECLARE @EventID int
DECLARE @CalendarID int
SELECT @EventID = EventID FROM Events.Occurances WHERE OccuranceID = @OccuranceID
SELECT @CalendarID = CalendarID FROM Events.Events WHERE EventID = @EventID

SELECT O.*
FROM Events.Occurances O
WHERE O.OccuranceID = @OccuranceID

SELECT E.*
FROM Events.Events E
WHERE E.EventID = @EventID

SELECT C.*
FROM Events.Calendars C
WHERE C.CalendarID = @CalendarID
", CommandType.Text,
    DB.CreateParameter("@OccuranceID", input.ID)))
            {
                dr.Read();
                data.ID = dr["OccuranceID"].DbInt();
                data.Title = dr["Title"].DbString();
                data.ShortDescription = dr["ShortDescription"].DbString();
                data.Description = dr["Description"].DbString();
                data.Location = dr["Location"].DbString();
                data.StartDate = dr["StartDate"].DbDate();
                data.EndDate = dr["EndDate"].DbDate();
                data.TimeZone = dr["TimeZone"].DbInt();
                data.IsAllDay = dr["IsAllDay"].DbBool();

                dr.NextResult();
                dr.Read();
                data.Event = new Shared.Entities.Events.Event();
                data.Event.ID = dr["EventID"].DbInt();
                data.Event.IsRepeat = dr["IsRepeat"].DbBool();

                dr.NextResult();
                dr.Read();
                data.Event.Calendar = new Shared.Entities.Events.Calendar();
                data.Event.Calendar.ID = dr["CalendarID"].DbInt();
                data.Event.Calendar.Name = dr["Name"].DbString();
                data.Event.Calendar.Description = dr["Description"].DbString();
                data.Event.Calendar.Color = dr["Color"].DbString();
            }

            return data;
        }
    }
}